package com.shujia.init

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment
import org.apache.flink.table.catalog.hive.HiveCatalog

object InitDwdTable {
  def main(args: Array[String]): Unit = {
    val bsEnv = StreamExecutionEnvironment.getExecutionEnvironment
    val bsSettings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build()

    val bsTableEnv = StreamTableEnvironment.create(bsEnv, bsSettings)

    val hiveCatalog = new HiveCatalog("myHive", "sent", "SentimentCompute/src/main/resources")

    bsTableEnv.registerCatalog("myHive", hiveCatalog)
    bsTableEnv.useCatalog("myHive")

    bsTableEnv.executeSql(
      """
        |create database if not exists ads
        |""".stripMargin)

    bsTableEnv.executeSql(
      """
        |use dwd
        |""".stripMargin)

    bsTableEnv.executeSql(
      """
        |drop table if exists dwd_kafka_weibo_msk
        |""".stripMargin)
    bsTableEnv.executeSql(
      """
        |CREATE TABLE dwd_kafka_weibo_msk (
        |  id String,
        |  comments_count bigint,
        |  created_at String,
        |  source String,
        |  reposts_count bigint,
        |  attitudes_count bigint,
        |  text String,
        |  user_id String
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'dwd_kafka_weibo_msk',
        | 'properties.bootstrap.servers' = 'node1:9092,node2:9092,master:9092',
        | 'properties.group.id' = 'asdasd',
        | 'format' = 'json',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'json.ignore-parse-errors' = 'true'
        |)
        |""".stripMargin)
    bsTableEnv.executeSql(
      """
        |drop table if exists dwd_kafka_user_msk
        |""".stripMargin)
    bsTableEnv.executeSql(
      """
        |CREATE TABLE dwd_kafka_user_msk (
        |  id String,
        |  gender String,
        |  screen_name String,
        |  followers_count bigint,
        |  follow_count bigint
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'dwd_kafka_user_msk',
        | 'properties.bootstrap.servers' = 'node1:9092,node2:9092,master:9092',
        | 'properties.group.id' = 'asdasd',
        | 'format' = 'json',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'json.ignore-parse-errors' = 'true'
        |)
        |""".stripMargin)

    bsTableEnv.executeSql(
      """
        |drop table if exists dwd_kafka_comment_msk
        |""".stripMargin)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE dwd_kafka_comment_msk (
        |  comment_id String,
        |  comment_time String,
        |  like_count bigint,
        |  comment_text String,
        |  user_id String,
        |  weibo_id String
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'dwd_kafka_comment_msk',
        | 'properties.bootstrap.servers' = 'node1:9092,node2:9092,master:9092',
        | 'properties.group.id' = 'asdasd',
        | 'format' = 'json',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'json.ignore-parse-errors' = 'true'
        | )
        |""".stripMargin)
  }

}
